package skuatools.tiles
{

	import com.google.maps.CopyrightCollection;
	import com.google.maps.TileLayerBase;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	
	/**
	 * @private
	 */
	public class TileMillTileLayer extends TileLayerBase 
	{
		/**
	   	* Constructor for the class.
	   	*/
	   	private var _url:String;
	  	
		public function TileMillTileLayer(alpha:Number, url:String) 
		{
	  		var copyCollection:CopyrightCollection = new CopyrightCollection();
	    	super(copyCollection,0,14,alpha);
	    	this._url = url;
	  	}
	
		/**
	   	* Creates and loads a tile (x, y) at the given zoom level.
	   	* @param tilePos  Tile coordinates.
	   	* @param zoom  Tile zoom.
	   	* @return  Display object representing the tile.
	   	*/
	  	public override function loadTile(tilePos:Point, zoom:Number):DisplayObject 
		{
	  		var tileLoader:Loader = new Loader();
			var urlRequest:URLRequest = new URLRequest(_url
				+ zoom + '/' 
				+ tilePos.x + '/'
				//the example I used didn't need this transformation that MapBox uses, however, other examples might
				//+ Math.abs(tilePos.y - (Math.pow(2, zoom) - 1)) + '.png');
				+ tilePos.y + '.png');
	    	tileLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	    	tileLoader.load(urlRequest);      
	    	return tileLoader;
	  	}
	  	private function ioErrorHandler(event:IOErrorEvent):void {
	    	//trace("ioErrorHandler: " + event);
	  	}
	}
}

